package com.greenant.store.view;

import java.io.File;
import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.greenant.store.R;
import com.greenant.store.utils.UtilTools;
import com.soundcloud.android.crop.Crop;

public class GetPicMethodDialog extends Dialog implements android.view.View.OnClickListener {
    private Activity mActivity;
    private Button mBtnTakePhoto;
    private Button mBtnFromLib;
    private Button mBtnCancel;

    public static final int RequestCodeTakePhoto = 1;

    public static String sCaptureStoreDir;
    public Uri mExtraOutputUri;

    public GetPicMethodDialog(Activity activity) {
        super(activity, R.style.choose_date_dialog_style);
        this.setContentView(R.layout.get_pic_method_dialog);
        this.mActivity = activity;

        mBtnTakePhoto = (Button) findViewById(R.id.get_pic_take_photo);
        mBtnFromLib = (Button) findViewById(R.id.get_pic_from_lib);
        mBtnCancel = (Button) findViewById(R.id.get_pic_cancel);

        mBtnTakePhoto.setOnClickListener(this);
        mBtnFromLib.setOnClickListener(this);
        mBtnCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.equals(mBtnCancel)) {
            this.dismiss();
        } else if (v.equals(mBtnTakePhoto)) {
            /*
             * 相机拍摄
             */
            try {
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    // 有外部存储（调用系统相机返回原始图片）
                    sCaptureStoreDir = Environment.getExternalStorageDirectory() + "/jxgTemp";
                    File storeDir = new File(sCaptureStoreDir);
                    if (!storeDir.exists()) {
                        storeDir.mkdirs();
                    }

                    File imgFile = new File(storeDir,
                            String.valueOf(Calendar.getInstance().getTimeInMillis()));
                    mExtraOutputUri = Uri.fromFile(imgFile);
                    Log.d("CEXX",
                            "CEXX => GetPicMethodDialog => mExtraOutputUri : " + mExtraOutputUri);

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, mExtraOutputUri);
                    intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                    mActivity.startActivityForResult(intent, RequestCodeTakePhoto);
                } else {
                    // 没有外部存储
                	UtilTools.showMyToast(mActivity, mActivity.getResources().getString(R.string.get_pic_no_sdcard));
                }
            } catch (ActivityNotFoundException e) {
                e.printStackTrace();
            }
            this.dismiss();
        } else if (v.equals(mBtnFromLib)) {
            /*
             * 从图库选择
             */
        	Crop.pickImage(mActivity);
            this.dismiss();
        }
    }
}
